<?php
	include_once('general_include.php');
	$date		= date('Y-m-d H:i:s');
	
	 $user_id_to=$_REQUEST['user_id_to'];
	 $user_id_from=$_SESSION['user_id'];
	 $reply_of_msg=$_REQUEST['reply_of_msg'];
	 $message_text=$_REQUEST['message_text'];
	 
	 		
	if($_REQUEST['flag']==1)
		{			
			/* Insert Into Comment Starts */
						
			$table_name = TABLEPREFIX."_message ";

			$fields_values = array( 
									'user_id_to'  			=> $user_id_to,
									'user_id_from'  		=> $user_id_from,										
									'reply_of_msg'  		=> $reply_of_msg,
									'message_text'  		=> $message_text,
									'date_added'  			=> $date
							    	);		
			
			$msgreport= $UserManagerObjAjax->InsertRecords($table_name,$fields_values);
			
			$message_id=mysql_insert_id();			

			/* Insert Into Comment Ends */
			
			/* send email notification starts  */
			    if($_SESSION['user_type'] =='1')
				  $type = 'Coach : ';
				else
				  $type = 'Student : ';  
			    
				$usertoSql="SELECT * FROM ".TABLEPREFIX."_user where user_id='".$user_id_to."'";
                $recordto = $UserManagerObjAjax->GetRecords("Row",$usertoSql);
			    
			    $tpl_file="email_templates/message_notification.html";
				$tpl_handler=fopen($tpl_file,"r");
				$tpl_message=fread($tpl_handler,filesize($tpl_file));
				fclose($tpl_handler);
			
				    $fromEmail	= "GEC Admin";
				    $to			= $recordto['email'];
			
			         
					$url=$site_url;
					$tpl_message=str_replace("[SITEURL]",$url,$tpl_message);
					$tpl_message=str_replace("[HEADING]",$recordto['name'],$tpl_message);
					$tpl_message=str_replace("[FROM]",$type.$_SESSION['name'],$tpl_message);
					$tpl_message=str_replace("[MESSAGE]",$message_text,$tpl_message);
				
					$mail = new PHPMailer;
					$mail->FromName = "GEC Admin";
					$mail->From    =  $fromEmail;
					$mail->Subject =  "Message from NOOBSKOOL";
					$mail->Body    = stripslashes($tpl_message);
					$mail->IsHTML(true);
					$mail->AddAddress($to,$to);
					
				   if($_SERVER['HTTP_HOST'] !='10.10.0.100')
				    $mail->Send();
			
			/* send email notification ends */
			
		
		$latest_msg_sub = "SELECT *,DATE_FORMAT(date_added , '%D %M %Y %r' ) AS date_add 
							FROM ".TABLEPREFIX."_message 
							WHERE reply_of_msg=".$reply_of_msg."
							ORDER BY message_id ";
		$latest_msg_subArr = $UserManagerObjAjax->GetRecords("All",$latest_msg_sub);
		
			for($j=0;$j<count($latest_msg_subArr);$j++)
			{
				$userImg2 = "SELECT name,photo FROM ".TABLEPREFIX."_user WHERE user_id='".$latest_msg_subArr[$j]['user_id_from']." ' ";
				$ImgArr2 = $UserManagerObjAjax->GetRecords("Row",$userImg2);
							
				$latest_msg_subArr[$j]['photo'] = $ImgArr2['photo'];	
				$latest_msg_subArr[$j]['name'] = $ImgArr2['name'];
				
			$resp.=' <div id="reply_content_'.reply_of_msg.'">		
					<div class="message-post">
					 ';
					 if($latest_msg_subArr[$j]['photo']!='')
					 {
					 $resp.='<div class="message-thumb"><img src="uploaded/user_images/thumbs/mid_'.$latest_msg_subArr[$j]['photo'].'" width="57" height="50" alt="" border="0" /></div>	';
					 }
					 else
					 {
					 $resp.='<div class="message-thumb"><img src="images/avatar_smallest.jpg" border="0" alt="" /></div>';
					 }
					 		 
					$resp.='<div style="float:left; display:block; width:380px;">
					<h3>'.$latest_msg_subArr[$j]['name'].'</h3>
					<span class="message-date">'.$latest_msg_subArr[$j]['date_add'].'</span>
					<br />'.$latest_msg_subArr[$j]['message_text'].'</div>
					<div class="clear"></div>
					</div>
					<div class="message-post-bottom"></div>
					<div class="clear"></div>
					</div>';
			}
			
			$resp.=' <div class="clear"></div>                        
					<p class="button-flex" id="reply_'.$reply_of_msg.'">
					<a href="javascript:;" onclick="showPostbox('.$reply_of_msg.')"><span>Reply</span></a>
					</p>';
			
			echo $resp;
			
			}
			
		if($_REQUEST['flag']==2)
		{			
			/* Insert Into Comment Starts */
						
			$table_name = TABLEPREFIX."_message ";

			$fields_values = array( 
									'is_read'  				=> 'Y',								
							    	);		
			
			$where='message_id='.$_REQUEST['message_id'];			
			$msgreport= $UserManagerObjAjax->UpdateRecords($table_name,$fields_values,$where);			
		}	
			
		if($_REQUEST['flag']==3)
		{			
			/* Insert Into Comment Starts */
			
								
			$table_name = TABLEPREFIX."_message ";

			/*$fields_values = array( 
									'is_read'  				=> 'Y',								
							    	);		*/
			
			$where=' (message_id='.$_REQUEST['message_id'].' OR reply_of_msg='.$_REQUEST['message_id'].') AND user_id_from  <> '.$_SESSION['user_id'] ;			
			//$msgreport= $UserManagerObjAjax->UpdateRecords($table_name,$fields_values,$where);			
			
			
			$sql1="UPDATE ".$table_name." SET is_read = 'Y' WHERE ".$where;
			$result1=mysql_query($sql1);
		}				
		
?>